﻿using System.ComponentModel;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Define;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner.MyControls
{
    /// <summary>
    /// 单行文本输入框
    /// </summary>
    [ToolboxItem(false)]
    public class SingleTextBox : MultiTextBox
    {
        public SingleTextBox(Model model) : this(model, EDesignModeState.Run) { }

        public SingleTextBox(Model model, EDesignModeState designModeState)
            : base(model, designModeState)
        {
            this.Multiline = false;
            _label_UnderLine = new Label();
            _label_UnderLine.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            _label_UnderLine.AutoSize = false;
            _label_UnderLine.Left = 0;
            _label_UnderLine.Top = this.PreferredHeight - 1;
            _label_UnderLine.Width = this.Width;
            _label_UnderLine.Height = 1;
            _label_UnderLine.Visible = false;
            this.Controls.Add(_label_UnderLine);
        }

        private Label _label_UnderLine = null;
        protected EBorderStyle _borderStyleZ = EBorderStyle.Fixed3D;

        public override EUISizeMode UISizeMode
        {
            get
            {
                return EUISizeMode.AllowWidth;
            }
        }

        public override GetCtrlProperty PropertyObject
        {
            get
            {
                if (this._propertyObject == null)
                {
                    this._propertyObject = new GetSingleTextBoxProperty(this);
                }

                return this._propertyObject;
            }
        }

        public override EBorderStyle BorderStyleZ
        {
            get
            {
                return this._borderStyleZ;
            }
            set
            {
                this._borderStyleZ = value;

                this.BorderStyle = BorderStyleConvert.ToBorderStyle(value);

                if (_borderStyleZ == EBorderStyle.UnderLine)
                {
                    _label_UnderLine.Visible = true;
                    _label_UnderLine.Top = this.PreferredHeight - 1;
                }
                else
                {
                    _label_UnderLine.Visible = false;
                }
            }
        }

        public override string ToXML()
        {
            return "<PasswordChar>" + this.PasswordChar.ToString().Replace("\0", "") + "</PasswordChar>" +
                       "<Base>" + base.ToXML() + "</Base>";
        }

        public override void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                foreach (System.Xml.XmlNode XN in xns)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "passwordchar":
                            if (XN.InnerText.Length > 0)
                            {
                                this.PasswordChar = char.Parse(XN.InnerText);
                            }
                            break;

                        case "base":
                            base.FromXML(XN.ChildNodes);
                            break;

                        default:
                            break;
                    }
                }
            }
        }
    }
}